package com.sduept.nwld.dataserver.sourceinfo.transform.impl;

import com.sduept.nwld.dataserver.sourceinfo.transform.TransformFunction;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SubString implements TransformFunction<String>{
	private Map<String,Object> params;
	public SubString(){
		if(params==null)
			params=new HashMap<String,Object>();	
	}

	@Override
	public Map<String,Object> getParams() {				
		return params;
	}

	@Override
	public String apply(Map<String,Object> params) {
		String sourceInfo="";
		Object fromIndex=null;
		boolean isFirst=true;
		char startChar=' ';
		int step=0;
		int beginIndex=0;
		int endIndex=0;
		if(params!=null){
				if(params.size()==2){
					for(String paramName:params.keySet()){					
						if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
							sourceInfo=params.get(paramName).toString();
						}else if("beginIndex".equals(paramName)&& params.get(paramName) instanceof Integer){
							beginIndex=Integer.valueOf(params.get(paramName).toString());
						}				
					}					
					return sourceInfo.substring(beginIndex);
				}else if(params.size()==3){
					for(String paramName:params.keySet()){					
						if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
							sourceInfo=params.get(paramName).toString();
						}else if("beginIndex".equals(paramName)&& params.get(paramName) instanceof Integer){
							beginIndex=Integer.valueOf(params.get(paramName).toString());
						}else if("endIndex".equals(paramName)&& params.get(paramName) instanceof Integer){
							endIndex=Integer.valueOf(params.get(paramName).toString());
						}					
					}					
					return sourceInfo.substring(beginIndex,endIndex);
				}else if(params.size()==4){				
					for(String paramName:params.keySet()){					
						if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
							sourceInfo=params.get(paramName).toString();
						}else if("isFirst".equals(paramName)&& params.get(paramName) instanceof Boolean){
							isFirst=(boolean)params.get(paramName);
						}else if("startChar".equals(paramName)&& params.get(paramName) instanceof Character){
							startChar=(char)params.get(paramName);
						}else if("step".equals(paramName)&& params.get(paramName) instanceof Integer){
							step=(int)params.get(paramName);								
						}					
					}
					if(sourceInfo!=null && !"".equals(sourceInfo) && sourceInfo.indexOf(startChar)!=-1){
						if(isFirst){
							if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar))
								return sourceInfo.substring(sourceInfo.indexOf(startChar),sourceInfo.indexOf(startChar)+step);
						}
						else{
							if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar))
								return sourceInfo.substring(sourceInfo.lastIndexOf(startChar),sourceInfo.lastIndexOf(startChar)+step);
						}
					}
			}else if(params.size()==5){
				for(String paramName:params.keySet()){					
					if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
						sourceInfo=params.get(paramName).toString();
					}else if("isFirst".equals(paramName)&& params.get(paramName) instanceof Boolean){
						isFirst=(boolean)params.get(paramName);
					}else if("fromIndex".equals(paramName)&& params.get(paramName) instanceof Character){
						fromIndex=(char)params.get(paramName);
					}else if("fromIndex".equals(paramName)&& params.get(paramName) instanceof String){
						fromIndex=params.get(paramName).toString();
					}else if("startChar".equals(paramName)&& params.get(paramName) instanceof Character){
						startChar=(char)params.get(paramName);
					}else if("step".equals(paramName)&& params.get(paramName) instanceof Integer){
						step=(int)params.get(paramName);								
					}	
				}
				if(sourceInfo.indexOf(startChar)!=-1){
					if(fromIndex instanceof Character){
						if(isFirst){
							if(sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex))>=0){
								if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex)))
									return sourceInfo.substring(sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex)),
										sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex))+step);
							}
						}
						else {
							if(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex))>=0){
								if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex)))
									return sourceInfo.substring(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex)),
										sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex))+step);
							}
						}
					}else if(fromIndex instanceof String){
						if(isFirst){
							if(sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))>=0){
								if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString())))
									return sourceInfo.substring(sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString())),
										sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))+step);
							}
						}
						else {
							if(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))>=0){
								if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString())))
									return sourceInfo.substring(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString())),
										sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))+step);
							}
						}
					}
				}
			}
		}
		return null;
	}

	public static void main(String[] args){
		SubString subString=new SubString();		
		String sourceInfo="100kV济南站";		
		boolean isFirst=true;
		char fromIndex='你';
		char startChar='吃';
		int step=3;
		int beginIndex=0;
		int endIndex=sourceInfo.indexOf("V")+1;
		subString.params.put("sourceInfo", sourceInfo);
	    subString.params.put("isFirst",isFirst);
	    subString.params.put("fromIndex",fromIndex);
	    subString.params.put("startChar",startChar);
	    subString.params.put("step",step);
		/*subString.params.put("beginIndex",beginIndex);
	    subString.params.put("endIndex",endIndex);*/
	    log.info(subString.apply(subString.params));
		
		String str1="100kV济南站";
		String str2=str1.substring(0, str1.indexOf('V')+1);
		log.info(str2);
	}
	

}
